package com.yfp.operation.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.common.datascope.annotation.DataScope;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.operation.domain.MarCouponPack;
import com.yfp.operation.domain.MarCouponPackShop;
import com.yfp.operation.domain.TagCombo;
import com.yfp.operation.mapper.MarCouponPackShopMapper;
import com.yfp.operation.mapper.TagComboMapper;
import com.yfp.operation.service.MarCouponPackService;
import com.yfp.operation.service.MarCouponPackShopService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * @description 针对表【mar_coupon_pack_shop(礼包销售)】的数据库操作Service实现
 * @createDate 2024-10-22 09:53:40
 */
@Service
public class MarCouponPackShopServiceImpl extends ServiceImpl<MarCouponPackShopMapper, MarCouponPackShop> implements MarCouponPackShopService {

    @Resource
    private MarCouponPackShopMapper marCouponPackShopMapper;
    @Resource
    private MarCouponPackService marCouponPackService;
    @Resource
    private TagComboMapper tagComboMapper;

    @Override
    @Transactional
    public void create(MarCouponPackShop marCouponPackShop) {
        // 锁定卡包销售库存
        MarCouponPack marCouponPack = marCouponPackService.findById(marCouponPackShop.getCouponPackId());
        marCouponPack.setShopCount(marCouponPackShop.getPackNum());
        marCouponPack.setSurplusCount(marCouponPack.getSurplusCount() - marCouponPack.getShopCount());
        marCouponPackService.updateById(marCouponPack);

        Long userId = SecurityUtils.getUserId();
        marCouponPackShop.setDeptId(SecurityUtils.getLoginUser().getSysUser().getDeptId());
        marCouponPackShop.setCreateUserId(userId);
        marCouponPackShop.setUpdateUserId(userId);
        marCouponPackShop.setSoldQuantity(0L);
        marCouponPackShopMapper.create(marCouponPackShop);
    }

    @Override
    @Transactional
    public void edit(MarCouponPackShop marCouponPackShop) {
        MarCouponPack marCouponPack = marCouponPackService.findById(marCouponPackShop.getCouponPackId());
        if (marCouponPackShop.getStatus() != -1 && marCouponPack.getShopCount() == 0L) {
            // 锁定卡包销售库存
            marCouponPack.setShopCount(marCouponPackShop.getCurrentStock() + marCouponPackShop.getSoldQuantity());
            marCouponPack.setSurplusCount(marCouponPack.getSurplusCount() - marCouponPackShop.getCurrentStock());
            marCouponPackShop.setStatus(0);
        } else if (marCouponPackShop.getStatus() != -1 && marCouponPack.getShopCount() != 0L) {
            // 锁定卡包销售库存
            MarCouponPackShop marCouponPackShop1 = marCouponPackShopMapper.findById(marCouponPackShop.getId());
            long currentStock = marCouponPackShop1.getCurrentStock() - marCouponPackShop.getCurrentStock();
            marCouponPack.setShopCount(marCouponPack.getShopCount() + currentStock);
            marCouponPack.setSurplusCount(marCouponPack.getSurplusCount() + currentStock);
            marCouponPackShop.setStatus(0);
        } else if (marCouponPackShop.getStatus() == -1) {
            // 删除，解锁定卡包销售库存
            MarCouponPackShop marCouponPackShop1 = marCouponPackShopMapper.findById(marCouponPackShop.getId());
            marCouponPack.setShopCount(0L);
            marCouponPack.setSurplusCount(marCouponPack.getSurplusCount() + marCouponPackShop1.getCurrentStock());
            marCouponPackShop.setCurrentStock(0L);
        }
        marCouponPackService.updateById(marCouponPack);

        Long userId = SecurityUtils.getUserId();
        marCouponPackShop.setUpdateUserId(userId);
        marCouponPackShopMapper.edit(marCouponPackShop);
    }

    @Override
    @DataScope(deptAlias = "cps")
    public List<MarCouponPackShop> list(MarCouponPackShop marCouponPackShop) {
        return marCouponPackShopMapper.list(marCouponPackShop);
    }

    @Override
    public MarCouponPackShop findById(Long id) {
        return marCouponPackShopMapper.findById(id);
    }

    @Override
    public List<MarCouponPackShop> findBySortAndStatus(Integer sort, Integer status, String cityName) {
        return marCouponPackShopMapper.findBySortAndStatus(sort, status, cityName);
    }

    @Override
    public MarCouponPackShop findByPackId(Long packId, Integer status) {
        return marCouponPackShopMapper.findByPackId(packId, status);
    }

    @Override
    @Transactional
    public void merchandize(Long id) {
        // 锁定卡包销售库存
        MarCouponPackShop marCouponPackShop = marCouponPackShopMapper.findById(id);
        marCouponPackShop.setStatus(1);
        marCouponPackShop.setCurrentStock(marCouponPackShop.getPackNum() - marCouponPackShop.getSoldQuantity());

        MarCouponPack marCouponPack = marCouponPackService.findById(marCouponPackShop.getCouponPackId());
        if (marCouponPack.getShopCount() == 0L) {
            // 从新扣库存
            marCouponPack.setShopCount(marCouponPackShop.getCurrentStock() + marCouponPackShop.getSoldQuantity());
            marCouponPack.setSurplusCount(marCouponPack.getSurplusCount() - marCouponPackShop.getCurrentStock());
            marCouponPackService.updateById(marCouponPack);
        }
        marCouponPackShopMapper.edit(marCouponPackShop);
    }

    @Override
    @Transactional
    public void unshelve(Long id) {
        MarCouponPackShop marCouponPackShop = marCouponPackShopMapper.findById(id);
        // 锁定卡包销售库存
        MarCouponPack marCouponPack = marCouponPackService.findById(marCouponPackShop.getCouponPackId());
        marCouponPack.setShopCount(0L);
        marCouponPack.setSurplusCount(marCouponPack.getSurplusCount() + marCouponPackShop.getCurrentStock());
        marCouponPackService.updateById(marCouponPack);

        marCouponPackShop.setStatus(2);
        marCouponPackShop.setCurrentStock(0L);
        marCouponPackShopMapper.edit(marCouponPackShop);
    }

    @Override
    public Integer findSort(String cityName) {
        Integer sort = marCouponPackShopMapper.findSort(cityName);
        return null == sort ? 1 : sort + 1;
    }

    @Override
    public TagCombo findTagComboById(String tagId) {
        return tagComboMapper.findById(tagId);
    }

    @Override
    public List<MarCouponPackShop> findByPackIds(List<Long> invalidMarCouponPackIds) {
        if (CollUtil.isEmpty(invalidMarCouponPackIds)) {
            return List.of();
        }
        return marCouponPackShopMapper.findByPackIds(invalidMarCouponPackIds);
    }

    @Override
    public void updateStatusByIds(List<Long> packShopIds, int status) {
        if (CollUtil.isEmpty(packShopIds)) {
            return;
        }
        marCouponPackShopMapper.updateStatusByIds(packShopIds, status);
    }
}